package com.gmail.blackdog1987.onedollar.dao;

import com.gmail.blackdog1987.onedollar.dao.hibernate.EntityHibernateDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.onedollar.entity.TGoodsShowCommentEntity;
import com.gmail.blackdog1987.onedollar.entity.TGoodsShowLikeEntity;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.vo.GoodsShowCommentVO;
import com.gmail.blackdog1987.onedollar.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("goodsShowCommentDao")
public class GoodsShowCommentDao extends EntityHibernateDao<TGoodsShowCommentEntity>{
    @Autowired
    private BaseJdbcDao baseJdbcDao;
    public boolean isComment(int userid, int showid){
        return baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) from t_goods_show_comment where user_id = ? and show_id = ?", new Object[]{userid, showid})>0;
    }

    public Page queryForPage(int index, int size, Integer id) {

        String limit = " limit " + (index-1)*size + "," + size;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) from t_goods_show_comment where show_id = ?" , new Object[]{id});

        List<GoodsShowCommentVO> list = JdbcUtil.queryForList(baseJdbcDao, GoodsShowCommentVO.class ," SELECT t_goods_show_comment.*, t_user.head, t_user.nick_name from t_goods_show_comment left join t_user on t_goods_show_comment.user_id = t_user.user_id where show_id = ? order by id desc " + limit, new Object[]{id});

        Page page = new Page(list, index, totalCount, size);

        return page;
    }
}
